package com.tekj.cxqcb.view.Module.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.logger.Logger;
import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * description: 服务建议图片适配器
 */
public class Proposal2Adapter extends RecyclerView.Adapter<Proposal2Adapter.ViewHolder> {


    private List<LocalMedia> mList = null;
    private Context mContext;
    private OnCallbackListener mOnCallbackListener;
    private int maxSize;
    //是否可以删除
    private boolean isdelete = true;
    private boolean isName = false;

    public Proposal2Adapter(Context context, List<LocalMedia> list, int maxSize) {
        this.mList = list;
        this.mContext = context;
        this.maxSize = maxSize;
    }

    public Proposal2Adapter(Context mContext, List<LocalMedia> mList, boolean isdelete) {
        this.mList = mList;
        this.mContext = mContext;
        this.maxSize = isdelete ? mList.size() + 1 : mList.size();
        this.isdelete = isdelete;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(mContext).inflate(R.layout.item_proposal2, null, false);
        return new ViewHolder(itemview);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        /**
         * 绑定数据到控件
         */
        if (mList != null) {
            if (position < mList.size()) {
                //存在真实数据状态
                String imgPath = mList.get(position).getPath();//未压缩的图片
                if (mList.get(position).isCompressed()) {
                    imgPath = mList.get(position).getCompressPath();//压缩后的图片
                }
                Logger.d("imgPath>>>" + imgPath);
                if (imgPath.contains("http")) {
                    Picasso.get()
                            .load(imgPath)
                            .into(holder.imgSel);
                } else {
                    Picasso.get()
                            .load("file:///" + imgPath)
                            .into(holder.imgSel);
                }

            }
        }


        /**
         * UI更新
         */
        String Msg;
        if (mList == null) {
            Msg = "第一张图片，添加图片";
            holder.imgDel.setVisibility(View.GONE);
            holder.imgAdd.setVisibility(View.VISIBLE);
//            holder.imgSel.setImageResource(R.mipmap.img_picture_up);
        } else {
            if ((position == getItemCount() - 1) && mList.size() < maxSize) {
                Msg = "添加图片";
                holder.imgDel.setVisibility(View.GONE);
                holder.imgAdd.setVisibility(View.VISIBLE);
//                holder.imgSel.setImageResource(R.mipmap.img_picture_up);
            } else {
                Msg = "已绑定的图片";
                holder.imgAdd.setVisibility(View.GONE);
                holder.imgDel.setVisibility(View.VISIBLE);
                holder.imgDel.setVisibility(isdelete ? View.VISIBLE : View.GONE);
            }

        }
        Logger.d("加载>>>" + Msg);

        /**
         * 点击图片预览或继续添加
         */
        holder.imgSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Msg;
                if (mList == null) {
                    Msg = "第一张图片，添加图片";
                    mOnCallbackListener.onAddPhotoClick();
                } else {
                    if ((position == getItemCount() - 1) && mList.size() < maxSize) {
                        Msg = "添加图片";
                        mOnCallbackListener.onAddPhotoClick();
                    } else {
                        Msg = "已绑定的图片";
                        mOnCallbackListener.onPicturePreview(position);
                    }
                }
                Logger.d("点击>>>" + Msg);

//                Toasty.info(mContext, Msg).show();
            }
        });

        /**
         * 点击删除图片
         */
        holder.imgDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Logger.d("点击删除图片>>>" + position);
                mOnCallbackListener.onPictureDelete(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        if (mList == null) {
            return 1;
        } else if (mList.size() == 0) {
            return 1;
        } else if (mList.size() > 0 && mList.size() < maxSize) {
            return mList.size() + 1;
        } else {
            return mList.size();
        }
    }

    public void setList(List<LocalMedia> list) {
        this.mList = list;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_sel)
        ImageView imgSel;
        @BindView(R.id.img_del)
        ImageView imgDel;
        @BindView(R.id.img_add)
        ImageView imgAdd;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    public interface OnCallbackListener {
        void onAddPhotoClick();

        void onPicturePreview(int position);

        void onPictureDelete(int position);
    }

    public void setOnCallbackListener(OnCallbackListener onCallbackListener) {
        this.mOnCallbackListener = onCallbackListener;
    }

    public void setList(List<LocalMedia> list, int maxsize) {
        this.mList = list;
        this.maxSize = maxsize;
        notifyDataSetChanged();
    }

    public boolean isIsdelete() {
        return isdelete;
    }

    public void setIsdelete(boolean isdelete) {
        this.isdelete = isdelete;
    }

    public boolean isName() {
        return isName;
    }

    public void setName(boolean name) {
        isName = name;
    }
}
